package com.guo.core.framework.arrange.version;

import com.guo.core.framework.arrange.*;
import com.guo.core.framework.arrange.checker.OrderSiteChecker;
import com.guo.core.framework.arrange.checker.OrderStatusChecker;
import com.guo.core.framework.arrange.checker.OrderTypeChecker;
import com.guo.core.framework.arrange.notice.DeLiObserver;
import com.guo.core.framework.arrange.notice.OmsObserver;
import com.guo.core.framework.arrange.notice.OpmsObserver;
import com.guo.core.framework.arrange.post.ArrangePost;
import com.guo.core.framework.arrange.post.MileagePost;
import com.guo.core.framework.arrange.post.ZTBMatchPost;
import com.guo.core.framework.chain.CheckChain;
import com.guo.core.framework.chain.PostChain;
import com.guo.core.framework.observable.BizSubject;
import com.guo.core.framework.template.BizService;
import com.guo.core.framework.template.ServiceEnum;
import com.guo.core.framework.template.ServiceType;
import com.guo.core.framework.template.ServiceTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class ArrangeServiceV2 extends ArrangeServiceTemplate {

    @Autowired
    private OrderTypeChecker orderTypeChecker;
    @Autowired
    private OrderStatusChecker orderStatusChecker;
    @Autowired
    private OrderSiteChecker orderSiteChecker;
    @Autowired
    private ZTBMatchPost ztbMatchPost;
    @Autowired
    private MileagePost mileagePost;
    @Autowired
    private ArrangePost arrangePost;
    @Autowired
    private OmsObserver omsObserver;
    @Autowired
    private DeLiObserver deLiObserver;
    @Autowired
    private OpmsObserver opmsObserver;

    @Override
    protected ServiceType getServiceType() {
        return ServiceTypeEnum.ARRANGE_V2;
    }

    @Override
    protected BizService getService() {
        return ServiceEnum.ARRANGE;
    }

    @Override
    protected ArrangeContext install(ArrangeReq arrangeReq) {
        // 1、组装上下文
        ArrangeContext context = new ArrangeContext();
        // 1.1、排车请求参数
        context.setArrangeReq(arrangeReq);

        // 1.2、排车响应数据
        ArrangeRsp arrangeRsp = new ArrangeRsp();
        context.setArrangeRsp(arrangeRsp);

        // 1.3、排车检查链
        CheckChain<ArrangeReq> checkChain = new CheckChain<>();
        checkChain.addChain(orderTypeChecker)
            .addChain(orderStatusChecker)
            .addChain(orderSiteChecker);
        context.setCheckChain(checkChain);

        // 1.4、排车执行链
        PostChain<ArrangeReq, ArrangeRsp> postChain = new PostChain<>();
        postChain.addChain(arrangePost);
        context.setPostChain(postChain);

        BizSubject<ArrangeNotice> bizSubject = new BizSubject<>();
        bizSubject.add(omsObserver);
        context.setBizSubject(bizSubject);

        return context;
    }

    @Override
    protected void after(ArrangeContext arrangeContext) {
        log.info("执行后置逻辑:\n{}", arrangeContext);

        ArrangeRsp arrangeRsp = arrangeContext.getArrangeRsp();
        if (Boolean.TRUE.equals(arrangeRsp.getSuccess())) {
            ArrangeNotice arrangeNotice = new ArrangeNotice();
            arrangeNotice.setCarNo(arrangeRsp.getCarNo());
            arrangeContext.getBizSubject().publish(arrangeNotice);
        }
    }

    @Override
    protected void init(ArrangeReq arrangeReq) {
        log.info("执行初始化逻辑:\n{}", arrangeReq);
    }

    @Override
    protected void before(ArrangeReq arrangeReq) {
        log.info("执行前置逻辑-组装业务框架:\n{}", arrangeReq);
    }
}
